//
//  YZLoadWeatherViewModel.m
//  kuaiyi
//
//  Created by jacoy on 16/5/5.
//
//

#import "YZLoadWeatherViewModel.h"
#import "YZNetWorkTool.h"
#define kBDWeather_KEY @"17IvzuqmKrK1cGwNL6VQebF9"
#define weatherURL @"http://api.map.baidu.com/telematics/v3/weather"

@implementation YZLoadWeatherViewModel
+(void)loadWeather:(NSString *)city{
    NSDictionary *parameters = @{@"location":city,@"output":@"json",@"ak":kBDWeather_KEY};
    
    [[YZNetWorkTool sharedNetWorkTool] GET:weatherURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
        
        if ([responseObject[@"status"] isEqualToString:@"success"]) {
            
            NSString *date = responseObject[@"date"];
            NSArray *results =responseObject[@"results"];
            NSDictionary *weatherdata = results[0];
            NSArray *weather_data = weatherdata[@"weather_data"];
            NSDictionary *today = weather_data[0];
            
            
            
            NSDictionary *weatherDict = @{@"city":city,@"temperature":today[@"temperature"],@"date":date,@"weather":today[@"weather"]};
            [[NSNotificationCenter defaultCenter]postNotificationName:YZWeatherNotification object:self userInfo:weatherDict];
        }else{

        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@",error);
        
    }];

}
@end
